package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author luomingliang
 * @since 2020-06-09
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    //课程分类列表(树形)
    @Override
    public List<OneSubject> getAllOneSubject() {
        //1.查询所有一级分类 parent_id=0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapperOne);
        //2.查询所有二级分类 parent_id<>0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapperTwo);

        //创建list集合, 用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        for(EduSubject e1: oneSubjects) {
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(e1.getId());
//            oneSubject.setTitle(e1.getTitle());
            BeanUtils.copyProperties(e1, oneSubject);
            //利用stream流进行筛选, 得到当前一级分类下的所有二级分类
            List<EduSubject> twoSubjectsOfOne = twoSubjects.stream()
                    .filter(x -> e1.getId().equals(x.getParentId()))
                    .collect(Collectors.toList());
            List<TwoSubject> twoSubjects2 = new ArrayList<>();

            for(EduSubject e2: twoSubjectsOfOne) {
                TwoSubject twoSubject = new TwoSubject();
//                twoSubject.setId(e2.getId());
//                twoSubject.setTitle(e2.getTitle());
                BeanUtils.copyProperties(e2, twoSubject);
                twoSubjects2.add(twoSubject);
            }
            //把当前一级分类下的所有二级分类设置进去, 然后加入最终返回的集合中
            oneSubject.setChildren(twoSubjects2);
            finalSubjectList.add(oneSubject);
        }

        //3.封装一级分类

        //4.封装二级分类
        return finalSubjectList;
    }


}
